//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
//_/_/
//_/_/  CADIA.Module.PitchTracker
//_/_/  Eric Nivel
//_/_/
//_/_/  Copyright(c)2007 Center for Analysis and Design of Intelligent Agents
//_/_/                   Reykjavik University
//_/_/                   All rights reserved
//_/_/
//_/_/                   http://cadia.ru.is/
//_/_/
//_/_/  Redistribution and use in source and binary forms, with or without
//_/_/  modification, is permitted provided that the following conditions 
//_/_/  are met:
//_/_/
//_/_/  - Redistributions of source code must retain the above copyright notice,
//_/_/    this list of conditions and the following disclaimer.
//_/_/
//_/_/  - Redistributions in binary form must reproduce the above copyright 
//_/_/    notice, this list of conditions and the following disclaimer in the 
//_/_/    documentation and/or other materials provided with the distribution.
//_/_/
//_/_/  - Neither the name of its copyright holders nor the names of its 
//_/_/    contributors may be used to endorse or promote products derived from 
//_/_/    this software without specific prior written permission.
//_/_/
//_/_/  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
//_/_/  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
//_/_/  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
//_/_/  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
//_/_/  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
//_/_/  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
//_/_/  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
//_/_/  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
//_/_/  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
//_/_/  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
//_/_/  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//_/_/
//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

#ifndef	WAVELET_PITCH_PROCESSOR_H
#define	WAVELET_PITCH_PROCESSOR_H

class PitchOut {
public:
	PitchOut(void){pitch=0;level=0;voiced=false;}
	~PitchOut(void){}
	double	pitch;
	double	signal_high;	//	abs(abs(max signal)-abs(average signal))
	double	signal_low;	//	abs(abs(average signal)-abs(min signal))
	double	signal_energy;
	int		level;
	bool		voiced;
} ;//struct moved out of class (and renamed and made a class) to make forward declarations possible

class	WaveletPitchProcessor{	//	C++ implementation of the pitch analysis (Matlab implementation: wavelet.m)

	private:

		static	const	unsigned	int	MaxFrequency=3000;
		static	const	unsigned	int	ApproximationLevels=6;	//	approximation count
		static	const	unsigned	int	DifferenceLevels=3;	//	vicinity radius

		const	unsigned	int	bufferSize;
		const	unsigned	int	samplingFrequency;

		int	maxCount[ApproximationLevels];
		int	minCount[ApproximationLevels];

		unsigned	int	*maxIndices;
		unsigned	int	*minIndices;

		double	*signal[ApproximationLevels];

		double	*differences;

		double	mode[ApproximationLevels];

	public:

		WaveletPitchProcessor(const	unsigned	int	bufferSize,
							  const	unsigned	int	samplingFrequency);
		~WaveletPitchProcessor();

		void	trackPitch(const short	*buffer,
			               const	double				amplitudeThreshold,
						   const	double				noiseThreshold,
						   PitchOut	*dataOut);

		void trackZeroCrossings(const short *buffer, PitchOut * dataOut);
};


#endif